<template>
  <div class="container">
    <div>
      <el-row :gutter="12">
        <el-col :span="6">
          <el-form ref="form" :model="form">
            <el-form-item label="人员名称：">
              <el-select v-model="form.workerName" placeholder="请选择" @change="selectWorkerName">
                <el-option
                  v-for="item in form.workerList"
                  :key="item.VID"
                  :label="item.NAME"
                  :value="item.VID"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="时间间隔：">
              <el-select v-model="form.alermTypeValue" placeholder="请选择">
                <el-option
                  v-for="item in form.alermTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-form ref="form" :model="form">
            <el-form-item label="开始日期：">
              <el-date-picker
                v-model="form.StartDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期：">
              <el-date-picker
                v-model="form.EndDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-form ref="form" :model="form" style="float:left">
            <el-button size="mini" type="primary" @click="getlistdata">查 询</el-button>
            <el-button size="mini" type="success" @click="exportExcel">导 出</el-button>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)"
      highlight-current-row
    >
      <el-table-column prop="人员姓名" label="人员姓名" align="center" width="150"></el-table-column>
      <el-table-column prop="定位时间" label="定位时间" align="center"></el-table-column>
      <el-table-column prop="经度" label="经度" align="center"></el-table-column>
      <el-table-column prop="纬度" label="纬度" align="center"></el-table-column>
      <el-table-column prop="速度" label="当时时速" align="center"></el-table-column>
      <el-table-column prop="位置" label="位置" align="center"></el-table-column>
      <!-- <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-info" @click="handleEdit(scope.$index, scope.row)">详情</el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import '@/assets/css/common.css'
import { formatDate } from '@/common/js/date.js'

import export2Excel from '@/utils/exportExcel/index.js'
export default {
  data () {

    return {
      currentPage: 1, //初始页
      pageSize: 10,    //    每页的数据
      form: {
        workerList: [],//车牌号列表
        workerName: '',//人员名称
        workerClientNO: '',//人员组号
        alermTypeList: [{ label: '1分钟', value: '1' }, { label: '5分钟', value: '5' }, { label: '10分钟', value: '10' }, { label: '30分钟', value: '30' }],
        alermTypeValue: '5',
        StartDate: formatDate(new Date(new Date() - 3600 * 1000 * 24), 'yyyy-MM-dd'),
        EndDate: formatDate(new Date(), 'yyyy-MM-dd'),
      },
      originalData: [],
      tableData: [],
      loading: false
    }
  },
  created () {
  },
  mounted () {
    // this.getlistdata()
    this.getLisencePlateList()
  },
  methods: {
    //获取车牌列表
    async getLisencePlateList () {
      var url = 'Worker/GetVehicleInfoList?ClientNo=' + localStorage.getItem('CLIENT_NO')
      const { data } = await this.$Http.get(url)
      this.form.workerList = data
      // this.form.workerList.unshift({ "LicensePlate": "全部" })
    },
    selectWorkerName (id) {
      console.log("select中对应value值")
      console.log(id)
      let selectedWorker = {};
      selectedWorker = this.form.workerList.find((item) => {//这里的chargingWorkNameList就是上面遍历的数据源
        return item.VID === id;
        //筛选出匹配数据，是对应数据的整个对象
      });
      console.log("select中对应label值")
      console.log(selectedWorker)
      this.form.workerName = selectedWorker.NAME
      this.form.workerClientNO = selectedWorker.CLIENT_NO
    },
    async getlistdata () {
      this.currentPage = 1;
      this.loading = true
      // var url = 'Vehicle/GetVehicleTracksForQuery?LicensePlate=津C607D5(华明)&timeSpan=30&StartDate=2021-06-01&EndDate=2021-06-03'
      // var url = 'Vehicle/GetVehicleTracksForQuery?StartDate=' + this.form.StartDate + '&EndDate=' + this.form.EndDate + '&timeSpan=' + this.form.alermTypeValue + '&LicensePlate=' + this.form.workerClientNO
      var url = 'Worker/GetWorkerTracksForQuery?Name='+ this.form.workerName + '&TeamNo=' + this.form.workerClientNO + '&StartDate=' + this.form.StartDate + '&EndDate=' + this.form.EndDate + '&timeSpan=' + this.form.alermTypeValue
      const { data } = await this.$Http.get(url)
      // 原始数据
      this.tableData = data
      // console.log(data)
      this.loading = false
      // this.personQuery()
    },
    // 人员管理查询
    // personQuery () {
    //   this.tableData = this.originalData
    //   // const { name, client } = this.form
    //   // if (this.form.workerClientNO != '全部') this.tableData = this.tableData.filter(item => item.LICENSE_PLATE.includes(this.form.workerClientNO))
    //   if (this.form.alermTypeValue != '全部') this.tableData = this.tableData.filter(item => this.form.alermTypeValue === item.TYPENAME)
    // },
    // 导出Excel
    exportExcel () {
      let excelName = '历史轨迹查询'
      const tHeader = ['人员姓名', '定位时间', '经度', '纬度', '当时时速', '位置']
      const filterVal = ['人员姓名', '定位时间', '经度', '纬度', '速度', '位置']
      export2Excel(tHeader, filterVal, this.tableData, excelName)
    },
    //详情
    handleEdit (index, row) {
      //   this.idx = index;
      //   this.form = row;
      //   this.editVisible = true;
    },
    // 分页导航
    handlePageChange (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)
      // this.$set(this.query, 'pageIndex', val);
      // this.getData();
    },
    handleSizeChange (size) {
      this.pageSize = size;
      console.log(this.pageSize)
    }
  }
}
</script>
<style>
.content {
  overflow-y: scroll;
}
.el-col-6 {
  width: 350px;
  min-height: 50px;
}
.el-table th {
  height: 40px;
}
.el-form-item label {
  width: 80px;
}

</style>